<?php
/**
 * Created by PhpStorm.
 * User: crstu
 * Date: 2016/4/28
 * Time: 10:37
 */

namespace backend\controllers;

use yii;
use common\models\CommonFunction;
use backend\commands\BaseAdminController;
use backend\models\Service\AdminService;

class LoginController extends BaseAdminController
{
    
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'minLength'    => 4,
                'maxLength'    => 5,
            ]
        ];
    }

    //前置操作
    public function beforeAction($action)
    {
        return parent::beforeAction($action);
    }

    /**
     * 登陆操作
     * @return string
     */
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $session = Yii::$app->session;

        if($session->get('ADMIN_ID') > 0){

            if($this->isMobile()){
                $this->redirect(['mobile/index'])->send();
            }else{
                $this->redirect(['index/index'])->send();
            }

        }else{
            if(!$request->isPost){

                //显示登陆页面
                $this->layout = 'login';

                //字段名前添加随机字符串前缀，防止攻击
                $input_suffix = CommonFunction::getRandChar(6);
                $session->set('INPUT_SUFFIX', $input_suffix);
                return $this->render('index', ['input_suffix' => $input_suffix]);

            } else {

                //登陆操作
                $input_suffix = $session->get('INPUT_SUFFIX');
                $adminName = $request->post('username_' . $input_suffix);
                $adminPass = $request->post('password_' . $input_suffix);
                $verifyCode = $request->post('code');
                if (empty($adminName) || empty($adminPass)) {
                    $this->renderJson(0, '', '请输入用户名或密码！');
                } elseif (empty($verifyCode)){
                    $this->renderJson(0, '', '请输入验证码！');
                }else{

                    //执行登录操作并返回消息
                    $result = (new AdminService())->login($adminName, $adminPass, $verifyCode);
                    if(is_array($result) && $result[0] >= 1){
                        $this->renderJson(1, '', '登陆成功！！');
                    }else{
                        $this->renderJson(0, '', $result[1]);
                    }

                }
            }
        }

        return false;
    }

    /**
     * 登出操作
     */
    public function actionOut()
    {
        session_start();
        session_destroy();
        $this->redirect(['/login']);
    }


    /**
     * 计算验证码结果
     */
    protected function calcCaptcha($captcha){
        preg_match_all('/(\d+)|(\+|\-)/', $captcha, $match);
        if(is_array($match[0]) && count($match[0]) == 3){
            if($match[0][1] == '+'){
                return intval($match[0][0] + $match[0][2]);
            }else{
                return intval($match[0][0] - $match[0][2]);
            }
        }
        return false;
    }
    
}